#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "process_manager.h"
#include "process.h"		//included for testing purposes
#include "util.h"

void menu();
void reset_string(char **string);

int main(int arg, char **argv)
{

	if (arg == 2)
	{
		if (is_same_string(argv[1], "menu", 4))
		{
			menu();
		} else
		{
			printf
			    ("Unknown Command, please enter 'menu' or nothing\n");
			return 0;
		}
	}
	//execute_management();

	/*setup the process manager memory */
	setup();

	execute_management();

	/*add new process */
    int i = 0;
    for(i; i<150; i++)
    {
            add_process();
    }

    for(i=0; i<400; i++)
    {
        execute_management();
    }

	return 0;
}

/*
 *A user menu for adding process and seeing how many are in use
 *
 * CMDs
 *
 * Create [PID]: create a process with the given pid
 */

void menu()
{
	int   i      = 0;
    int   k      = 0;
    char* string = calloc(50, sizeof(char));

    /* Initialize the processes manager */
    setup();

	while (1)
	{
        printf("Please enter a command:\n");

        for(i=0; i<50; i++)
        {
            string[i] = fgetc(stdin);
            if(string[i] == '\n')
            {
                break;
            }
        }
   
        //A bit of a hack, should look into how to do this better
        //without a goto :-P [use if-else statements]

        /* i = is_same_string(string, "create", 6);*/

        /* Create a Process */
        if(is_same_string(string, "create", 6))
        {
            /* process number */
            i = atoi( (string+6) );

            /* find group number */
            k = 7; // start of first digit of process number 
            while(string[k] != ' ' && string[k] != '\n')
            {
                k++;
            }

            /* group number */
            k = atoi( (string+k) );
            
            //i = add_process_pid(i);
            if( -1 == i )
            {
                printf("unable to add the process, already in a queue\n");

            }
			else if (k != 0)
			{
                printf("added the process <%d> to the ready queue in group <%d>\n", i, k);
                
                add_process_pid_gid(i, k);

            }
			else
			{
                printf("please enter in a process group and process number,\n"
                       "groups should be between 1-10 and process should \n"
                       "be between 0-127\n\n");
                
            }

            reset_string(&string); 
        }

        /* List a Process */
        else if(is_same_string(string, "list", 4))
        {
            if(is_same_string( (string+5), "all", 3))
            {
                list_all();
                            
                reset_string(&string);
            }

            else if(is_same_string( (string+5), "sched", 5))
            {
                list_sched();

                reset_string(&string);
            }

            else if(atoi((string+5)))
            {
                list_queue(i);

                reset_string(&string);
            }

            printf("Was unable to understand your list command\n");
            reset_string(&string);
        }

        /* Go, Manage Processes */
        else if(is_same_string(string, "go", 2))
        {
            if(is_same_string( (string+3), "unwait", 6))
            {
                i = atoi( (string+9) );
                printf("unwait %d\n", i);
                            
                reset_string(&string);
            }

            else if(is_same_string( (string+3), "eoquantum", 9))
            {
                i = atoi( (string+12) );
                printf("eoquantum %d\n", i);

                reset_string(&string);
            }
            
            else if(is_same_string( (string+3), "eolife", 6))
            {
                i = atoi( (string+9) );
                printf("eolife %d\n", i);
                            
                reset_string(&string);
            }

            else if(is_same_string( (string+3), "wait", 4))
            {
                i = atoi( (string+7) );
                printf("waiting %d\n", i);

                reset_string(&string);
            }
        }
	}
}

void reset_string(char **string)
{
	memset(string, ' ', 50);
}
